/**
  ******************************************************************************
  * @file    USART_app.c 
  * @author  Thongnh
  * @version V1.0
  * @date    18-07-2011
  * @brief   Main process of USART
  ******************************************************************************
  * @attention
  *
  * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
  * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
  * TIME. AS A RESULT, XICUTELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
  * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
  * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
  * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
  *
  * <h2><center>&copy; COPYRIGHT 2011 Xicutelectronics</center></h2>
  ******************************************************************************
  */ 

#include "includes.h"

#ifdef USE_PC

/* Private variables ---------------------------------------------------------*/
extern uint8_t Send_Buffer[];
extern uint8_t Receive_Buffer[];
extern uint8_t PrevXferComplete;

__IO uint8_t   PackedLength = 0;

/*******************************************************************************
* Function Name  : USB_LP_CAN1_RX0_IRQHandler
* Description    : This function handles USB Low Priority or CAN RX0 interrupts
*                  requests.
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
void USB_LP_CAN1_RX0_IRQHandler(void)
{
    USB_Istr();
}

/*******************************************************************************
* Function Name  : PCDriverInit
* Description    : This function initialize USB driver and USB HID.
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
void PCDriverInit(void)
{
    GPIO_InitTypeDef  GPIO_InitStructure;
    /* Enable the USB disconnect GPIO clock */
    RCC_AHBPeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
    /* USB_DISCONNECT used as USB pull-up */
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
    GPIO_Init(GPIOA, &GPIO_InitStructure);

    USB_Interrupts_Config();
    Set_USBClock();
    USB_Init();
}


/*******************************************************************************
* Function Name  : PCDriverCheckPackedIncome
* Description    : This function check income packed from USB
*                  requests.
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
uint8_t PCDriverCheckPackedIncome(uint8_t * data)
{
    uint8_t temp_return = 0;
    uint8_t i, k;
    if ( 0 < PackedLength )
    {   
        for ( i = 0, k = 1; i < IN_REPORT_SIZE; i++, k++)
        {
            data[i] = Receive_Buffer[k];
        }
        temp_return = PackedLength;
        PackedLength = 0;
    }
    return temp_return;
}



void PC_MsgIncome(uint8_t *type, uint8_t len1, uint8_t *data, uint8_t len2)
{
    uint8_t i, k;
    
    // wait for old packed is sent
    while( 0 == PrevXferComplete );
    
    //prepare data to send
    Send_Buffer[0] = OUT_REPORT_ID;

    // memcpy(TxBuffer, type, len1);
    for ( i = 0, k = 1; i < len1; i++, k++)
    {
        Send_Buffer[k] = type[i];
    }
    
    // memcpy(&TxBuffer[len1], data, len2);
    for ( i = 0, k = len1 + 1; i < len2; i++, k++)
    {
        Send_Buffer[k] = data[i];
    }
    
    /* Write the descriptor through the endpoint */
    USB_SIL_Write(EP1_IN, (uint8_t*) Send_Buffer, OUT_REPORT_SIZE);  
    SetEPTxValid(ENDP1);
    
    PrevXferComplete = 0;
}

/*******************************************************************************
Function name: PCDriverSendPacked
Decription: Send packed on USB
Input: None
Output: None
*******************************************************************************/
void PCDriverSendPacked(uint8_t *data, uint8_t len)
{
    uint8_t i, k;
    
    // wait for old packed is sent
    while( 0 == PrevXferComplete );
    
    //prepare data to send
    Send_Buffer[0] = OUT_REPORT_ID;
    for ( i = 0, k = 1; i < len; i++, k++)
    {
        Send_Buffer[k] = data[i];
    }
    
    /* Write the descriptor through the endpoint */
    USB_SIL_Write(EP1_IN, (uint8_t*) Send_Buffer, OUT_REPORT_SIZE);  
    SetEPTxValid(ENDP1);
    
    PrevXferComplete = 0;
}

#endif

// end of file

